//
//  MineViewController.m
//  Hui
//
//  Created by HawkingWang on 16/10/26.
//  Copyright © 2016年 Hui. All rights reserved.
//

#import "MineViewController.h"
#import "EditNameView.h"//修改姓名View
#import "PaymentMethodView.h"//充值View
#import "PickUpAddressViewController.h"//取货地址Controller
#import "RecruitDiliverymanViewController.h"//招募配送员Controller
#import "IncomeDetailViewController.h"//收入明细Controller
#import "WebViewController.h"//网页Controller



@interface MineViewController () <UIImagePickerControllerDelegate, UINavigationControllerDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
/**是否为配送员*/
@property (weak, nonatomic) IBOutlet UIView *deliveryView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *deliveryConstraint;//160.5****0
/**用户信息*/
@property (weak, nonatomic) IBOutlet UIButton *userIcon;
@property (weak, nonatomic) IBOutlet UILabel *userName;
@property (weak, nonatomic) IBOutlet UILabel *userIdentity;
@property (weak, nonatomic) IBOutlet UIView *identityView;
@property (weak, nonatomic) IBOutlet UILabel *userPhone;
/**账户*/
@property (weak, nonatomic) IBOutlet UILabel *money;
/**配送信息*/
@property (weak, nonatomic) IBOutlet UILabel *vehicles;
@property (weak, nonatomic) IBOutlet UILabel *income;


@end

@implementation MineViewController
{
    EditNameView *_editView;
    PaymentMethodView *_rechargeView;
    
    UIImage *_image;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = APPCOLOR;
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    CGRect rect=CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [SELECTCOLOR CGColor]);
    CGContextFillRect(context, rect);
    _image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    //设置导航栏
    [self setNav];
    [self setScrollView];
    
    //设置信息
    [self setInfo];
}
- (void)viewWillAppear:(BOOL)animated{
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
}
- (void)setNav{
    
    _identityView.layer.masksToBounds = YES;
    _identityView.layer.cornerRadius = 2;
    _identityView.layer.borderColor = [UIColor whiteColor].CGColor;
    _identityView.layer.borderWidth = 0.5;
}
- (void)setScrollView{
    _scrollView.showsVerticalScrollIndicator = NO;
}
#pragma mark === 设置基本信息 ===
- (void)setInfo{
//    [_userIcon setImage:<#(nullable UIImage *)#> forState:<#(UIControlState)#>];
//    _userName.text =
//    _userIdentity.text =
//    _userPhone.text =
    
//    _money.text =
//    _vehicles.text = 
//    _income.text =
}

#pragma mark === 点击事件 ===
/**修改头像*/
- (IBAction)uploadIcon:(id)sender {
    UIAlertController *alter = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    //选择、更改头像
    UIAlertAction *defaultAction1 = [UIAlertAction actionWithTitle:@"图库" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        // 显示照片选择控制器
        UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];
        
        [imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
        // 2) 是否允许编辑
        [imagePicker setAllowsEditing:YES];
        
        // 3) 设置代理
        imagePicker.delegate = self;
        
        imagePicker.navigationBar.translucent = NO;
        imagePicker.navigationBar.tintColor = [UIColor whiteColor];
        imagePicker.navigationBar.barTintColor = APPCOLOR;
        
        NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
        attrs[NSForegroundColorAttributeName] = [UIColor whiteColor];
        [[UINavigationBar appearance] setTitleTextAttributes:attrs];
        // 4) 显示照片选择控制器，显示modal窗口
        [self presentViewController:imagePicker animated:YES completion:nil];
        
    }];
    UIAlertAction *defaultAction2 = [UIAlertAction actionWithTitle:@"相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        // 显示照片选择控制器
        UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];
        [imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
        [imagePicker setAllowsEditing:YES];
        imagePicker.delegate = self;
        imagePicker.navigationBar.tintColor = [UIColor blackColor];
        [self presentViewController:imagePicker animated:NO completion:nil];
    }];
    [alter addAction:cancelAction];
    [alter addAction:defaultAction1];
    [alter addAction:defaultAction2];
    [self presentViewController:alter animated:NO completion:nil];
}
/**修改昵称*/
- (IBAction)editName:(id)sender {
    _editView = [[[NSBundle mainBundle] loadNibNamed:@"EditNameView" owner:nil options:nil] lastObject];
    _editView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    _editView.backgroundColor = RGBACOLOR(0, 0, 0, 0.5);
    _editView.heightConstraint.constant = 113.5 * SCREEN_HEIGHT / 667;
    _editView.nickName.text = _userName.text;
    [[UIApplication sharedApplication] .keyWindow addSubview:_editView];
    
    __weak __typeof__ (EditNameView *)weakView = _editView;
    __weak __typeof__ (self)weakSelf = self;
    _editView.editViewClick = ^(int num){
        switch (num) {
            case 0:
                [weakView removeFromSuperview];
                break;
            case 1:
                DLog(@"修改成功");
            {
                [weakView removeFromSuperview];
                weakSelf.userName.text = weakView.nickName.text;
                [SVProgressHUD showSuccessWithStatus:@"修改成功"];
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [SVProgressHUD dismiss];
                });
            }
            default:
                break;
        }
    };
}
/**充值*/
- (IBAction)recharge:(id)sender {
    UIButton *button = sender;
    [button setBackgroundImage:_image forState:UIControlStateSelected];
    
    _rechargeView = [[[NSBundle mainBundle] loadNibNamed:@"PaymentMethodView" owner:nil options:nil] lastObject];
    _rechargeView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    _rechargeView.backgroundColor = RGBACOLOR(0, 0, 0, 0.5);
    _rechargeView.heightConstraint.constant = 113.5 * SCREEN_HEIGHT / 667;
    [[UIApplication sharedApplication].keyWindow addSubview:_rechargeView];
    
    __weak __typeof__ (PaymentMethodView *)weakView = _rechargeView;
    _rechargeView.paymentViewClick = ^(int num){
        switch (num) {
            case 0:
                [weakView removeFromSuperview];
                break;
            case 1:
                DLog(@"充值成功, 刷新");
            {
                [weakView removeFromSuperview];
                [SVProgressHUD showSuccessWithStatus:@"充值成功"];
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [SVProgressHUD dismiss];
                });
            }
                break;
            default:
                break;
        }
    };
}
/**取货地址*/
- (IBAction)address:(id)sender {
    UIButton *button = sender;
    [button setBackgroundImage:_image forState:UIControlStateSelected];
    
    PickUpAddressViewController *pickUp = [[PickUpAddressViewController alloc] init];
    [self.navigationController pushViewController:pickUp animated:YES];
}
/**配送收入*/
- (IBAction)income:(id)sender {
    UIButton *button = sender;
    [button setBackgroundImage:_image forState:UIControlStateSelected];
    
    IncomeDetailViewController *income = [[IncomeDetailViewController alloc] init];
    [self.navigationController pushViewController:income animated:YES];
}
/**是否接单*/
- (IBAction)ifGetOrder:(id)sender {
}
/**招募配送员*/
- (IBAction)recruitDiliveryman:(id)sender {
    UIButton *button = sender;
    [button setBackgroundImage:_image forState:UIControlStateSelected];
    
    RecruitDiliverymanViewController *recruit = [[RecruitDiliverymanViewController alloc] init];
    [self.navigationController pushViewController:recruit animated:YES];
}
/**配送说明*/
- (IBAction)deliveryNotes:(id)sender {
    UIButton *button = sender;
    [button setBackgroundImage:_image forState:UIControlStateSelected];
    
    WebViewController *web = [[WebViewController alloc] init];
    web.titleStr = @"配送说明";
    [self presentViewController:web animated:YES completion:nil];
}
/**配送考试*/
- (IBAction)deliveryTest:(id)sender {
    UIButton *button = sender;
    [button setBackgroundImage:_image forState:UIControlStateSelected];
    
    WebViewController *web = [[WebViewController alloc] init];
    web.titleStr = @"配送考试";
    [self presentViewController:web animated:YES completion:nil];
}
/**客服中心*/
- (IBAction)service:(id)sender {
    UIButton *button = sender;
    [button setBackgroundImage:_image forState:UIControlStateSelected];
    
    WebViewController *web = [[WebViewController alloc] init];
    web.titleStr = @"客服中心";
    [self presentViewController:web animated:YES completion:nil];
}
/**用户指南*/
- (IBAction)guide:(id)sender {
    UIButton *button = sender;
    [button setBackgroundImage:_image forState:UIControlStateSelected];
    
    WebViewController *web = [[WebViewController alloc] init];
    web.titleStr = @"用户指南";
    [self presentViewController:web animated:YES completion:nil];
}
/**关于我们*/
- (IBAction)about:(id)sender {
    UIButton *button = sender;
    [button setBackgroundImage:_image forState:UIControlStateSelected];
    
    WebViewController *web = [[WebViewController alloc] init];
    web.titleStr = @"关于我们";
    [self presentViewController:web animated:YES completion:nil];
}
/**法律条款*/
- (IBAction)legal:(id)sender {
    UIButton *button = sender;
    [button setBackgroundImage:_image forState:UIControlStateSelected];
    
    WebViewController *web = [[WebViewController alloc] init];
    web.titleStr = @"法律条款";
    [self presentViewController:web animated:YES completion:nil];
}

#pragma mark ===UIImagePicker代理方法 ===
//照片选择完成的代理方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    // 获取编辑后的照片
    UIImage *image = [info[@"UIImagePickerControllerEditedImage"] fixOrientation];
    
    [picker dismissViewControllerAnimated:YES completion:^{
        
        //        [SVProgressHUD show];
//        //上传图片
//        [UpLoadImageManager uploadImage:image withCompltedBlock:^(BOOL success, NSString *urlString) {
//            if (success) {
//                if (_model.image.count == 0) {
//                    _uploadImageString = urlString;
//                }else {
//                    _uploadImageString = [NSString stringWithFormat:@"%@,%@", _model.imageString, urlString];
//                }
//                
//                
//                //上传图片
//                [self uploadStoreImages];
//            }else{
//        
//            }
//        }];
        [_userIcon setImage:image forState:UIControlStateNormal];
    }];
}
//压缩图片
- (UIImage *) imageCompressForWidth:(UIImage *)sourceImage targetWidth:(CGFloat)defineWidth
{
    CGSize imageSize = sourceImage.size;
    CGFloat width = imageSize.width;
    CGFloat height = imageSize.height;
    CGFloat targetWidth = defineWidth;
    CGFloat targetHeight = (targetWidth / width) * height;
    UIGraphicsBeginImageContext(CGSizeMake(targetWidth, targetHeight));
    [sourceImage drawInRect:CGRectMake(0,0,targetWidth,  targetHeight)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}


- (IBAction)close:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

//- (void)close{
//    [self.navigationController popViewControllerAnimated:NO];
//}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
